package main

import (
	"context"
	"fmt"
	"os"
	"strconv"

	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
)

func main() {
	p, _ := rocketmq.NewProducer(producer.WithNameServer([]string{"120.24.67.46:9876"}))
	/* 以下方法也可以使用, 未来会被废弃
		p, _ := rocketmq.NewProducer(
		producer.WithNsResolver(primitive.NewPassthroughResolver([]string{"120.24.67.46:9876"})),
		producer.WithRetry(2),
	)
	*/
	err := p.Start()

	if err != nil {
		fmt.Printf("生成producer失败: %s", err.Error())
		os.Exit(1)
	}
	topic := "test"

	for i := 0; i < 3; i++ {
		msg := &primitive.Message{
			Topic: topic,
			Body:  []byte("Nice 啊! " + strconv.Itoa(i)),
		}
		res, err := p.SendSync(context.Background(), msg)
		if err != nil {
			fmt.Printf("send message error: %s\n", err)
		} else {
			fmt.Printf("send message success: result=%s\n", res.String())
		}
	}
	err = p.Shutdown()
	if err != nil {
		fmt.Printf("shutdown producer error: %s", err.Error())
	}

}
